package redisex

import (
	"github.com/garyburd/redigo/redis"
	"log"
	"sync"
)

var redisPool *redis.Pool

var p_address string
var p_password string
var one sync.Once=sync.Once{}

func Init(address string,password string)  {

	one.Do(func() {
		p_address=address
		p_password=password

		if redisPool==nil{
			redisPool = &redis.Pool{
				MaxIdle:16,
				MaxActive:0,
				IdleTimeout:300,
				Dial:connectRedis,
			}

		}
	})


}

// 拨号函数
// address = host:port
func connectRedis() (redisConn redis.Conn , err error){

	options:=redis.DialPassword(p_password)
	redisConn, err = redis.Dial("tcp", p_address,options)
	if err != nil {
		log.Fatal(err.Error())
		panic(err)

	}

	return
}
func GetRedisInstance()  redis.Conn{
	return redisPool.Get()
}

func GetRedisPool() *redis.Pool {
	return redisPool
}


//redisConn.Do("SET","key","value","EX","10")//时间是秒
//命令 SET  GET    DEL   EXPIRE EXISTS
// expire second
func SetCacheEx(key string,value interface{},expire int) (err error) {
	_,err=GetRedisInstance().Do("SET",key,value,"EX",expire)
	return
}

func SetCache(key string,value  interface{}) (err error) {
	_,err=GetRedisInstance().Do("SET",key,value)
	return
}
func GetCache(key string) ([]byte,error) {
	//return redis.Bytes(GetRedisInstance().Do("GET",key))
	reply,err:=GetRedisInstance().Do("GET",key)
	if err!=nil{
		return nil,err
	}
	if reply==nil{
		return nil,nil
	}
	return redis.Bytes(reply,err)
}

func GetStringCache(key string) (string,error) {
	return redis.String(GetRedisInstance().Do("GET",key))
}

func GetIntCache(key string) (int,error) {
	return redis.Int(GetRedisInstance().Do("GET",key))
}

func GetInt64Cache(key string) (int64,error) {
	reply,err:=GetRedisInstance().Do("GET",key)
	if err!=nil{
		return 0,err

	}

	if reply==nil{
		return 0,nil
	}

	return redis.Int64(reply,err)
}

/*
 存储结构体类型
 */
func SetCacheHM(key string,value interface{},expire int) (err error){
	_,err=GetRedisInstance().Do("HMSET",redis.Args{key}.AddFlat(value),"EX",expire)
	return
}

/*
获取结构体类型,但是读不出数据
 */
func GetCacheHM(key string,obj interface{}) (error) {
	value,err:=redis.Values(GetRedisInstance().Do("HGETALL",key))
	if err != nil {
		return err
	}

	err = redis.ScanStruct(value, obj)
	if err != nil {
		return err
	}
	return nil
}

/*
删除 key
 */
func DelCache(key string) error {
	_,err:=GetRedisInstance().Do("DEL",key)
	return err
}

/*
设置过期时间
 */
func ExpireCache(key string,expire int) error {
	_,err:=GetRedisInstance().Do("EXPIRE",key,expire)
	return err
}

// Set 类型
func Sadd(key string,value string)  error{
  _,err:=GetRedisInstance().Do("SADD",key,value)
  return err
}

func SaddByte(key string,value []byte) error  {
	_,err:=GetRedisInstance().Do("SADD",key,value)
	return err
}

// Set 类型获取
func Smember(key string) ([]interface{},error){
	reply,err:=redis.Values(GetRedisInstance().Do("SMEMBERS",key))
	return reply,err
}

// Set 中的值移除
func Srem(key string,value ...string)  error{
	_,err:=GetRedisInstance().Do("SREM",key,value)
	return err
}

// 返回Set，并转成 []string
func SmemberString(key string) ([]string,error){
	value,err:=Smember(key)
	if err != nil {
		return nil,err
	} else {

		str_arr:=make([]string,0)
		for _, v := range value {
			b:=v.([]byte)
			str_arr=append(str_arr,string(b))
		}
		return str_arr,nil
	}

}
